# -- coding: utf-8 --
# @time : 2023/4/27
# @author : 周梦泽
# @file : Title2Excel.py
# @software: pycharm
"""
将标题写入excel
"""
from autoTask.taobao.sycm.gjc.gjc_hyc_robot.parseExcelToTitle import ParseExcelToTitle, TargetProduct
import pandas as pd
from common.utils.ExcelUtil import to_excel_auto_column_weight


def title2excel(excel_path, params):
    """
    将标题写入excel
    :param params: 上传的参数
    :param excel_path: excel路径
    :return:
    """
    target_product_map = params.get("targetProductMap")
    if target_product_map:
        target_product_list = [TargetProduct.build_by_param(target) for target in target_product_map]
        title_list = []
        user_cut_list = []
        for target in target_product_list:
            title, user_cut = ParseExcelToTitle(excel_path).parse_action(target)
            title_list.append(title)
            user_cut_list.append(user_cut)
        title_and_productid_list = [
            *zip(
                (target.product_id for target in target_product_list),
                title_list,
                user_cut_list
            )
        ]
        # print(title_and_productid_list)
        df = pd.DataFrame(title_and_productid_list, columns=['商品ID', '商品标题', '标题组成词'])
        # 创建一个ExcelWriter对象
        writer = pd.ExcelWriter(excel_path, engine='openpyxl', mode='a')
        # df.to_excel(writer, index=False, sheet_name='生成的商品标题')
        to_excel_auto_column_weight(df, writer, '生成的商品标题')
        writer.close()

